import { find } from '../../../../api/member'
import { orderList as agentOrderList, paymentByWarehourse } from '../../../../api/order'
import { reject, cancel } from '../../../../api/orderAdmin'
const util = require('../../../../utils/util.js')
let app = getApp();
Page({
  data: {
    orderData: [],
    currentTab: 0,
    pageNum: 1,
    pageSize: 5,
    memberId: '',
    orderManagement: '我推广的订单',
    orderType: 1,
    optionsHidden: false,
    vipType:app.globalData.userVipType,
    queryDate:'',
    startDate:'',
    endDate:'',
    fields:'',
    querryTypeName:'',
    arrDateType: [
      {value: 'day', name: '按日'},
      {value: 'month', name: '按月'},
      {value: 'year', name: '按年'}
    ],
    hidden:false,
  },
  onLoad(options) {
    if (options == null) {
      options = {}
    }
    if (options.id != null) {
      this.data.memberId = options.id
      if (options.name) {
        this.data.orderManagement = options.name + '推广的订单'
      }
      this.setData({
        memberId: this.data.memberId,
        orderManagement: this.data.orderManagement,
      })
    } else {
      this.setData({
        optionsHidden: !this.data.optionsHidden,
      })
    }
    this.loadOrder()
  },
  onShow(){
    this.data.fields = 'day'
    this.data.startDate =this.formatData("day",new Date()) 
    this.data.endDate =this.formatData("day",new Date()) 
    this.setData({
      querryTypeName:"按日",
      fields:this.data.fields,
      startDate:this.data.startDate,
      endDate:this.data.endDate,
    })
  },
  refresh() {
    this.triggerEvent('refresh')
  },
  detailsClick(e) {
    wx.navigateTo({
      // url: '/pages/member/order/details?id=' + e.currentTarget.dataset.id + '&extension=false',
      // url: '/pages/order/jumpPage/order/details?id=' + e.currentTarget.dataset.id+ '&type="read"',
      url: `/pages/order/jumpPage/order/details?id=${e.currentTarget.dataset.id}&type=read`
    })
  },
  payForHim(e) {
    var _this = this
    wx.showModal({
      title: '提示',
      content: '是否确认接单',
      success(res) {
        if (res.confirm) {
          paymentByWarehourse({ orderId: e.currentTarget.dataset.item.id }).then(res => {
            _this.data.orderData.splice(e.currentTarget.dataset.index, 1)
            _this.setData({
              orderData: _this.data.orderData
            })
          })
        }
      }
    })
  },
  swichNav: function (e) {
    this.data.pageNum = 1
    this.data.orderData = []
    var that = this;
    if (this.data.currentTab == e.currentTarget.dataset.current) {
      return false;
    } else {
      if (e.currentTarget.dataset.current == 0) {
        that.setData({
          myInvitation: this.data.myInvitation,
          currentTab: e.currentTarget.dataset.current
        })
        this.loadOrder()
      } else if (e.currentTarget.dataset.current == 1) {
        that.setData({
          currentTab: e.currentTarget.dataset.current
        })
        this.selectComponent('#statistics').getCardList();
      } else if (e.currentTarget.dataset.current == 2) {
        that.setData({
          currentTab: e.currentTarget.dataset.current
        })
        this.selectComponent('#performance').getCardList();
      }
    }
  },
  
  loadOrder() {
    if (this.data.pageNum == 1) {
      this.data.orderData = []
    }
    let params = {}
    params.pageNum = this.data.pageNum
    params.pageSize = this.data.pageSize
    params.memberId = this.data.memberId
    agentOrderList(params).then(res => {
      res.data.data.forEach(item => {
        this.data.orderData.push(item)
      })
      this.setData({
        orderData: this.data.orderData
      })
    })
  },

  cancel(e) {
    wx.showModal({
      title: '您需要关闭该订单嘛？',
      complete: (res) => {
        if (res.cancel) {
          wx.showToast({
            title: '已取消',
            icon: "none"
          })
        }
        if (res.confirm) {
          cancel(e.currentTarget.dataset.id).then(res => {
            if (res.type == 'success') {
              wx.showToast({
                title: res.content,
                icon: res.type
              })
            }
          }).catch(error => {
            wx.showToast({
              title: error.content,
              icon: "none",
            })
          })
        }
      }
    })
  },
  reject(e) {
    wx.showModal({
      title: '您需要驳回该订单嘛？',
      complete: (res) => {
        if (res.cancel) {
          wx.showToast({
            title: '已取消',
            icon: "none"
          })
        }
        if (res.confirm) {
          reject(e.currentTarget.dataset.id).then(res => {
            if (res.type == 'success') {
              wx.showToast({
                title: res.content,
                icon: res.type
              })
            }
          }).catch(error => {
            wx.showToast({
              title: error.content,
              icon: "none",
            })
          })
        }
      }
    })
  },
  oppenClick(){
    this.setData({
      hidden:!this.data.hidden,
    })
    setTimeout(()=>{
      this.setData({
        hidden:false,
      })
    },3000)
  },
  queryTypeClick(e){
    let fields = e.currentTarget.dataset.type
    this.setData({
      hidden:false,
      querryTypeName:e._relatedInfo.anchorTargetText,
      fields:fields
    })
  },
    //选择日期
    selectstartDate(e) {
      let date = e.detail.value;
      this.data.startDate = this.formatData(this.data.fields, date)
      this.setData({
        startDate:this.data.startDate,
      })
    },
    selectendDate(e) {
      let date = e.detail.value;
      this.data.endDate = this.formatData(this.data.fields, date)
      this.setData({
        endDate:this.data.endDate,
      })
    },

    //格式化时间 
  formatData: function (type, date) {
    var date = new Date(date);
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    var D = year + "-" + month + '-' + day
    var M = year + "-" + month 
    var Y = year

    switch (type) {
      case "day": return D;
        break;
      case "month": return M;
        break;
      case "year": return Y;
        break;
      default: [year, month,day].map(formatNumber).join('-')
    }
  },
  queryClick(){
    this.selectComponent('#performance').getCardList()
  },

  onPullDownRefresh: function () {
    wx.showNavigationBarLoading()
    this.data.pageNum = 1
    setTimeout(() => {
      wx.hideNavigationBarLoading()
      if (this.data.currentTab == 1) {
        this.selectComponent('#statistics').onPullDownRefresh();
      } else if (this.data.currentTab == 2) {
        this.selectComponent('#performance').onPullDownRefresh();
      }
      else if (this.data.currentTab == 0) {
        this.loadOrder()
      }
      wx.stopPullDownRefresh()
    }, 1000);
  },
  onReachBottom: function () {
    if (this.data.currentTab == 1) {
      this.selectComponent('#statistics').onReachBottom();
    } else if (this.data.currentTab == 2) {
      this.selectComponent('#performance').onReachBottom();
    } else if (this.data.currentTab == 0) {
      this.data.pageNum += 1
      this.loadOrder()
    }
  },
})
